﻿// Author: Iulian Lita
// Project: ErrorManager
// Path: C:\Users\Tym\Documents\Visual Studio 2008\Projects\proiectIP\ErrorManager
// Creation date: 1/12/2009 4:11 AM
// Last modified: 1/12/2009 4:12 AM

#region Using directives
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
#endregion

namespace Error
{
    /// <summary>
    /// Error management
    /// </summary>
    public static class ErrorManagement
    {
        private static object _ob = new object();

        /// <summary>
        /// Create log file
        /// </summary>
        public static void CreateLogFile()
        {
            StreamWriter sw = new StreamWriter("Log.txt");
            DateTime dt = DateTime.Now;
            sw.WriteLine("*****************************************");
            sw.WriteLine("***************Library***************");
            sw.WriteLine("*****************************************");
            sw.WriteLine("Log file created on " + dt.ToLocalTime() + ".");
            sw.WriteLine();
            sw.Close();
        } // CreateLogFile()

        private static void AppendException(Exception exc)
        {
            Monitor.Enter(_ob);
            StreamWriter sw = new StreamWriter("Log.txt", true);
            DateTime dt = DateTime.Now;
            sw.WriteLine(dt.Month + "/" + dt.Day + "/" + dt.Year + " " + dt.Hour + ":" + dt.Minute + ":" + dt.Second + ":" + dt.Millisecond + " -> " + exc.Message);
            sw.Close();
            Monitor.Exit(_ob);
        } // AppendException(exc)

        /// <summary>
        /// Manage exception
        /// </summary>
        /// <param name="exc">Exc</param>
        public static void ManageException(Exception exc)
        {
            if (!File.Exists("Log.txt"))
            {
                CreateLogFile();
            }
            AppendException(exc);
        } // ManageException(exc)
    } // class ErrorManagement
} // namespace Error
